package xFGE.Timer
{
	import flash.events.EventDispatcher;
	import flash.utils.getTimer;

	public class xTimerObj extends EventDispatcher
	{
		//最新记录时间
		private var _lastTime:int;
		//回调函数
		private var _callBackFun:Function;
		//内部次数记录
		private var _timesRecord:int;
		//限制次数
		private var _timerTimes:int;
		//时间间隔
		private var _interval:int;
		//计时key
		public var TimerKey:String;
		//计时器状态
		private var _timerState:String;
		//计时器内部计数器
		private var _counter:int;
	
		
		public function xTimerObj(key:String,interval:int,callback:Function,times:int=-1)
		{
			//TODO: implement function
			super();
			TimerKey = key;
			_timerTimes = _timesRecord = times;
			_interval = interval;
			_callBackFun = callback;
			_lastTime = getTimer();
			_timerState = xTimer.TIMER_STATE_ALIVE;
			_counter = 0;
		}
		
		/**
		 * 更新Timer 
		 * @param time
		 * 
		 */
		public function Update(time:int):void{
			if(_timerState != xTimer.TIMER_STATE_ALIVE) return;
			var delayTime:int = time - _lastTime;
			if(delayTime >= _interval && (_timerTimes == xTimer.TIMER_TIMES_UNLIMITED || _timerTimes >0)){
				if(_callBackFun != null) _callBackFun(this);
				_lastTime += delayTime;
				if(_timerTimes != -1)_timerTimes --;
				_counter++;
			}else if(_timerTimes == 0){
				_timerState = xTimer.TIMER_STATE_DEATH;
				dispatchEvent(new xTimerEvent(xTimerEvent.XTIMER_DEATH));
			}
		}
		
		/**
		 * 重置当前设置计时器 
		 * 
		 */
		public function Reset():void
		{
			if(_timerState != xTimer.TIMER_STATE_ALIVE) _timerState = xTimer.TIMER_STATE_ALIVE;
			_timerTimes = _timesRecord;
			_counter = 0;
			_lastTime = getTimer();
		}
		/**
		 * 重新设置间隔和次数 
		 * @param interval
		 * @param times
		 * 
		 */
		public function Renew(interval:int,times:int):void
		{
			_timerState = xTimer.TIMER_STATE_DEATH;
			_interval = interval;
			_timerTimes = _timesRecord = times;
			_counter = 0;
			Active();
		}
		/**
		 * 重新激活计时器 
		 * 
		 */
		public function Active():void{
			if(_timerState == xTimer.TIMER_STATE_DEATH){
				_lastTime = getTimer();
				_timerTimes = _timesRecord;
				_counter = 0;
			}
			_timerState = xTimer.TIMER_STATE_ALIVE;
		}
		
		/**
		 * 暂停计时器 
		 * 
		 */
		public function Pause():void{
			if(_timerState == xTimer.TIMER_STATE_ALIVE)
				_timerState = xTimer.TIMER_STATE_PAUSE;
		}
		
		/**
		 * 获取计时器状态 
		 * @return 
		 * 
		 */
		public function get timerState():String{
			return _timerState;
		}
		
		/**
		 * 获取剩下次数 
		 * @return 
		 * 
		 */
		public function get timerTimes():int{
			return _timerTimes;
		}
		
		/**
		 * 获取计数器 
		 * @return 
		 * 
		 */
		public function get counter():int{
			return _counter;
		}
		
		/**
		 * 返回时间间隔 
		 * @return 
		 * 
		 */
		public function get interval():int{
			return _interval;
		}
		
		/**
		 * 当前计时器的重复次数设置 
		 * @return 
		 * 
		 */
		public function get timesRecord():int{
			return _timesRecord;
		}
	}
}